package com.hhs.storage.bean;

import com.baomidou.mybatisplus.annotation.TableField;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang.StringUtils;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @author hhs
 * @since 2023-01-12
 */
@EqualsAndHashCode
@Data

public class BaseDepartment implements Serializable ,Comparable<BaseDepartment>{

    private static final long serialVersionUID = 1L;

    private String id;

    private String pid;

    private String name;
    //用于上下级关系的查询；部门等级level的计算
    private String ancestor;
    private String description;

    @TableField(exist = false)//不是数据库的字段
    private List<BaseDepartment> children;

    @TableField(exist = false)//部门及下及人数
    private Integer count=0;

    @TableField(exist = false)//上级部门数据
    private BaseDepartment parent;
    //是否是部门负责人
    @TableField(exist = false)
    private boolean isPrincipal;
    /**
     * 类型 1 负责人  2部门职员
     */
    @TableField(exist = false)//不是数据库的字段
    private Integer type;

    public static List<BaseDepartment> getChild(String id, List<BaseDepartment> allMenu) {
        //子级  
        List<BaseDepartment> childList = new ArrayList<BaseDepartment>();
        for (BaseDepartment nav : allMenu) {// 遍历所有节点，将所有的父id与传过来的根节点的id比较     
            //相等说明：为该根节点的子节点。      
            if (id.equals(nav.getPid())) {
                childList.add(nav);
            }
        }
        //递归   
        for (BaseDepartment nav : childList) {
            nav.setChildren(getChild(nav.getId(), allMenu));
        }
        //如果节点下没有子节点，返回一个空List（递归退出）  
        if (childList.size() == 0) {
            return new ArrayList<BaseDepartment>();
        }
        return childList;
    }

    /**
     * 部门tree上下级格式化
     * @param allMenu
     * @return
     * @throws IOException
     */
    public static List<BaseDepartment> format(List<BaseDepartment> allMenu,String pid) {
        //获取一级  
        List<BaseDepartment> rootMenu = new ArrayList<BaseDepartment>();
        for (BaseDepartment nav : allMenu) {
            if(StringUtils.isEmpty(pid)) {
                if (StringUtils.isEmpty(nav.getPid()) || "0".equals(nav.getPid())) {//父节点是0的，为根节点。        
                    rootMenu.add(nav);
                }
            }else{
                if (pid.equals(nav.getId())) {
                    rootMenu.add(nav);
                }
            }
        }
        //为一级设置子级，getClild是递归调用的    
        for (BaseDepartment nav : rootMenu) {
            List<BaseDepartment> childList = getChild(nav.getId(), allMenu);
            nav.setChildren(childList);
        }
        return rootMenu;
    }

    public Integer getLevel(){
        if( StringUtils.isEmpty(this.getAncestor())){
            return 1;
        }else{
           return  this.getAncestor().split(",").length;
        }
    }

    //部门排序，ancestors越短排序越靠前 小于0，升序排序
    @Override
    public int compareTo(BaseDepartment o) {
        return this.getLevel()-o.getLevel();
    }
}
